﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RpgXna.Model;
using RpgXna.Model.Entities.MainCharacter;
using RpgXna.Model.GameManager;
using RpgXna.Model.Helpers;
using RpgXna.Model.Interfaces;
using RpgXna.Model.Interfaces.ContextMenu;
using RpgXna.Model.Interfaces.InterfacesManagement;
using RpgXna.Model.Interfaces.Inventory;
using RpgXna.Model.Interfaces.MainMenu;
using RpgXna.Model.Menu;
using RpgXna.Model.Menu.MenuBehaviors;
using RpgXna.Model.XnaClassesWrappers;

namespace RpgXna
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        private readonly GraphicsDeviceManager _graphics;
        private Menu _menu;
        private SpriteBatch _sb;
        private Scene _scene;
        private KeyboardState _kbState;
        private MouseState _mState;
        private Texture2D _cursor;
        private InputManager _input;
        private InteractionsManager _im;
        private Inventory _inventory;
        private CmiFactory _cmi;
        private ContextMenu _ctMenu;
        private GameStateManager _gsm;
        private EnvironmentRepository _env;
        private Character _c;
        private InterfacesManager _interfaces;
        //private List<IInterface> _interfaces;
        public Game()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            _graphics.PreferredBackBufferHeight = 720;
            _graphics.PreferredBackBufferWidth = 1280;
            //_graphics.IsFullScreen = true;
            _graphics.ApplyChanges();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            _interfaces = new InterfacesManager();
            _input = new InputManager();
            _gsm = new GameStateManager();
            _menu = new Menu(_graphics, new InterfaceComponent(_gsm,100), _gsm, _input);
            _ctMenu = new ContextMenu(new InterfaceComponent(_gsm,50), _input);
            _cmi = new CmiFactory();
            _inventory = new Inventory(new InterfaceComponent(_gsm,10), _graphics, _input);
            _c = new Character(_input);
            _env = new EnvironmentRepository();
            _im = new InteractionsManager(_gsm, _ctMenu, _env, _c, _inventory, _cmi);
            _scene = new Scene(_c, _env);

            _interfaces.Add(_ctMenu);
            _interfaces.Add(_inventory);
            _interfaces.Add(_menu);


            base.Initialize();

            _menu.Show();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _sb = new SpriteBatch(GraphicsDevice);

            _menu.Load(Content);
            _scene.LoadScene(Content);
            _cursor = Content.Load<Texture2D>("menu_cursor");
            _ctMenu.Load(Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            _mState = Mouse.GetState();
            _kbState = Keyboard.GetState();

            var ksw = new KeyboardStateWrapper(_kbState);

            _input.Update(_mState, ksw);

            _interfaces.Update(ksw);

            if (_gsm.State == GameStates.ExitRequested)
                Exit();

            if (!_interfaces.AnyVisible())
                _scene.Update(gameTime, _mState, ksw);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            switch (_gsm.State)
            {

                case GameStates.NotStarted:
                    GraphicsDevice.Clear(Color.Black);
                    break;
                default:
                    GraphicsDevice.Clear(Color.LightGreen);
                    break;
            }

            _sb.Begin();

            if (_gsm.State == GameStates.Running)
            {
                _scene.Draw(_sb);

                _inventory.Draw(_sb);

                _ctMenu.Draw(_sb);
            }
            _menu.Draw(_sb);


            _sb.Draw(_cursor, new Vector2(_mState.X, _mState.Y), Color.White);


            _sb.End();

            base.Draw(gameTime);
        }
    }
}